<template>
  <el-dialog
    :title="!dataForm.type ? '新增' : '修改'"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    :lock-scroll="false"
    :visible.sync="visible"
  >
    <!--  <div class="mod-config"> -->
    <el-form
      v-loading="loading"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      class="alt_form"
      label-width="80px"
    >
      <el-form-item label="字典分类" prop="type">
        <el-input v-model="dataForm.type" placeholder="字典分类"></el-input>
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input
          v-model="dataForm.typeName"
          placeholder="类型名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="mod-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button :disabled="loading" type="primary" @click="dataFormSubmit()"
        >确定</el-button
      >
    </div>
    <!--  </div> -->
  </el-dialog>
</template>

<script>
import { updateType } from '@/api/sys/dict'
export default {
  data() {
    return {
      loading: false,
      visible: false,
      dataForm: {
        otype: "",
        type: "",
        typeName: ""
      },
      dataRule: {
        type: [
          { required: true, message: "字典分类不能为空", trigger: "blur" }
        ],
        typeName: [
          { required: true, message: "类型名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(type, typeName) {
      this.dataForm.type = type;
      this.dataForm.otype = type;
      this.dataForm.typeName = typeName;
      this.loading = false;
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.loading = true;
          
          updateType({
              id: this.dataForm.otype,
              type: this.dataForm.type,
              typeName: this.dataForm.typeName
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1000
                });
                this.visible = false;
                this.$emit("refreshDataList");
              } else {
                this.$message.error(data.msg);
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    }
  }
};
</script>
